package com.hschinese.pronunciation.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.bean.UserInfo;
import com.hschinese.pronunciation.utils.SharedPreferenceUtils;
import com.hschinese.pronunciation.widget.Header;

public class MoreActivity extends BaseActivity implements View.OnClickListener {
    private Header mHeader = null;
    private View mAboutView = null;
    private View mFeedBackView = null;
    private TextView mLogoutView = null;
    private TextView mAccountView = null;
    private ImageView mAvatarView = null;
    private TextView mCreateProfile;

    private SharedPreferenceUtils mSharedPrefUtil;
    private UserInfo mUserInfo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more);
        mSharedPrefUtil = SharedPreferenceUtils.getInstance(MoreActivity.this);
        if (mSharedPrefUtil.getIsLogined()) {
            mUserInfo = mSharedPrefUtil.getuserInfo();
        }
        initViews();
        if (mUserInfo != null)
        loadImage(mAvatarView, mUserInfo.getAvatars());
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mSharedPrefUtil.getIsLogined()) {
            mLogoutView.setText(getResources().getString(R.string.logout_btn_str));
            mAccountView.setText(mUserInfo.getName());
        } else {
            mLogoutView.setText(getResources().getString(R.string.login));
        }
    }

    private void initViews() {
        mHeader = (Header)findViewById(R.id.more_head);
        mHeader.setTitle(getResources().getString(R.string.more_info));
        mHeader.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mAboutView = findViewById(R.id.more_about_view);
        mAboutView.setOnClickListener(this);

        mFeedBackView = findViewById(R.id.more_about_feedback);
        mFeedBackView.setOnClickListener(this);

        mLogoutView = (TextView)findViewById(R.id.more_logout_btn);
        mLogoutView.setOnClickListener(this);

        mAccountView = (TextView)findViewById(R.id.more_user_account);
        mAvatarView = (ImageView)findViewById(R.id.more_user_icon);

        mCreateProfile = (TextView)findViewById(R.id.more_user_create_profile);
        mCreateProfile.setOnClickListener(this);
        if (mUserInfo != null && !mUserInfo.getAnonymity())
            mCreateProfile.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.more_about_view) {
            Intent it = new Intent(MoreActivity.this, AboutActivity.class);
            startActivity(it);
        } else if (id == R.id.more_about_feedback) {
            sendMailByIntent();
        } else if (id == R.id.more_logout_btn) {
            if (mSharedPrefUtil.getIsLogined()) {
                logout();
            } else { // TODO 到登录页面
                Intent it = new Intent(MoreActivity.this, LoginActivity.class);
                startActivity(it);
            }
        } else if (id == R.id.more_user_create_profile) {
            // 创建匿名账户的资料
            Intent it = new Intent(MoreActivity.this, RegisterActivity.class);
            it.putExtra("is_create_profile", true);
            startActivity(it);
        }

    }

    private void logout() {
        mSharedPrefUtil.setIsLogined(false);
        mSharedPrefUtil.setUserInfo(new UserInfo());
        Intent it = new Intent(MoreActivity.this, MainActivity.class);
        it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(it);
        finish();
    }

    public int sendMailByIntent() {
        String[] reciver = new String[] { "feedback@hschinese.com" };
        String[] mySbuject = new String[] { getResources().getString(R.string.feedback_btn_str) };
        String mybody = "";
        Intent myIntent = new Intent(android.content.Intent.ACTION_SEND);
        myIntent.setType("plain/text");
        myIntent.putExtra(android.content.Intent.EXTRA_EMAIL, reciver);
        myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySbuject);
        myIntent.putExtra(android.content.Intent.EXTRA_TEXT, mybody);
        startActivity(Intent.createChooser(myIntent, getResources().getString(R.string.feedback_btn_str)));
        return 1;
    }

    private void loadImage(ImageView view, String url) {
        RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
        ImageLoader imageLoader = new ImageLoader(mQueue, new BitmapCache());
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(view, R.drawable.ic_user_head, R.drawable.ic_user_head);
        //指定图片允许的最大宽度和高度
        imageLoader.get(url,listener, 200, 200);
    }

    class BitmapCache implements ImageLoader.ImageCache {
        private LruCache<String, Bitmap> cache;
        public BitmapCache() {
            cache = new LruCache<String, Bitmap>(8 * 1024 * 1024) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };
        }
        @Override
        public Bitmap getBitmap(String url) {
            return cache.get(url);
        }
        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            cache.put(url, bitmap);
        }
    }
}
